package org.bastion.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bastion.example.domain.Amount;
import org.bastion.example.domain.Client;
import org.bastion.example.domain.Period;
import org.bastion.example.domain.Vehicle;
import org.bastion.example.domain.VehicleType;

public class Fixturer {

	public static VehicleType VEHICLE_TYPE_CAR;
	public static VehicleType VEHICLE_TYPE_4WD;
	public static VehicleType VEHICLE_TYPE_LUXURY;
	public static VehicleType VEHICLE_TYPE_VAN;
	
	private VehicleType[] vehicleTypes;
	
	private List<Vehicle> vehicles;

	public void makeFixture() {
		vehicleTypes = new VehicleType[4];
		VEHICLE_TYPE_CAR = new VehicleType("Car/Convertible", new Amount("40.0"));
		vehicleTypes[0] = VEHICLE_TYPE_CAR;
		VEHICLE_TYPE_4WD = new VehicleType("4WD/Minibus", new Amount("60.0"));
		vehicleTypes[1] = VEHICLE_TYPE_4WD;
		VEHICLE_TYPE_LUXURY = new VehicleType("Luxury", new Amount("55.0"));
		vehicleTypes[2] = VEHICLE_TYPE_LUXURY;
		VEHICLE_TYPE_VAN = new VehicleType("Commercial Van", new Amount("75.0"));
		vehicleTypes[3] = VEHICLE_TYPE_VAN;
		makeVehiclesFixture();
		makeClientsFixture();
	}

	private void makeVehiclesFixture() {
		try {
			vehicles = new ArrayList<Vehicle>();
			BufferedReader in = getFileReader("org/bastion/test/vehicle_models.txt");
			String model = in.readLine();
			while (model != null) {
				VehicleType vt = vehicleTypes[(int) (Math.random() * vehicleTypes.length)];
				vehicles.add(new Vehicle(getRandomRegistration(), model, vt));
				
				if (Math.random() > 0.8) {
					model = in.readLine();
				}
			}
			in.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	private String getRandomRegistration() {
		
		return (int) (Math.random() * 90 + 10) + "-" + (char) (Math.random() * 26 + 65) + (char) (Math.random() * 26 + 65) + "-" + (int) (Math.random() * 90 + 10);
	}

	private void makeClientsFixture() {
		try {
			BufferedReader in = getFileReader("org/bastion/test/last_names.txt");
			String name = in.readLine();
			while (name != null) {
				Client client = new Client(name);
				makeReservations(client);
				
				if (Math.random() > 0.1) {
					name = in.readLine();
				}
			}
			in.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	private void makeReservations(Client client) {
		for (int i = 0; i < (Math.random() * 3 + 1); i++) {
			Vehicle vehicle = getRandomVehicle();
			Period period = new Period(getRandomDateBetween20080101And20100101(), (int) (Math.random() * 30));
			vehicle.reserve(period, client);
		}
	}

	private Vehicle getRandomVehicle() {
		return vehicles.get((int) (Math.random() * vehicles.size()));
	}

	private BufferedReader getFileReader(String fileName) {
		return new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(fileName)));
	}
	
	private Date getRandomDateBetween20080101And20100101() {
		return new Date((long) (Math.random() * 63158400000L + 1199142000000L));
	}
	
}
